package com.itheima.sc.service.pms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.sc.config.ComException;
import com.itheima.sc.vo.BrandVo;
import com.itheima.sc.vo.PageResult;
import com.itheima.sc.api.pms.BrandApi;
import com.itheima.sc.pojo.pms.Brand;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BrandService{

    @DubboReference
    private BrandApi brandApi;

    //添加品牌
    public void create(Brand brand) {
        brandApi.save(brand);
    }

    //删除品牌
    public void deleteById(Integer id) {
        //删除品牌判断,如果有产品,不能删除
        Brand brand = brandApi.getById(Convert.toLong(id));
        if (ObjectUtil.isNotEmpty(brand.getProductCount())){
            throw new ComException("当前品牌含有商品,不可删除");
        }
        brandApi.removeById(Convert.toLong(id));
    }

    //批量删除品牌
    public void deleteByIds(List<Long> ids) {
        //删除品牌判断,如果有产品,不能删除
        List<Brand> brands = brandApi.listByIds(ids);
        for (Brand brand : brands) {
            if (ObjectUtil.isNotEmpty(brand.getProductCount()) && brand.getProductCount()>0 ){
                throw new ComException("当前品牌含有商品,不可删除");
            }
        }
        brandApi.removeByIds(ids);
    }

    //根据品牌名称分页获取品牌列表
    public PageResult listByPage(String keyword, Integer pageNum, Integer pageSize) {
        List<BrandVo> brandVoList = brandApi.listByPage(keyword,pageNum,pageSize);
        //根据品牌名称查询totalCount
        Integer total = brandApi.getcount(keyword);
        Integer totalPage = total/pageNum+1;
        //封装pageResult
        PageResult<BrandVo> pageResult = new PageResult<>(pageNum,pageSize,total,totalPage,brandVoList);
        return pageResult;
    }

    //获取全部品牌列表
    public List<BrandVo> listAll() {
        List<Brand> brandList = brandApi.list();
        ArrayList<BrandVo> brandVos = new ArrayList<>();
        for (Brand brand : brandList) {
            BrandVo brandVo = new BrandVo();
            BeanUtil.copyProperties(brand,brandVo);
            brandVo.setId(Convert.toInt(brand.getId()));
            brandVos.add(brandVo);
        }
        return brandVos;
    }

    //批量更新厂家制造商状态
    public void updateFactoryStatus(Integer factoryStatus, List<Integer> ids) {
        brandApi.updateFactoryStatus(factoryStatus,ids);
    }

    //批量更新显示状态
    public void updateShowStatus(Integer showStatus, List<Integer> ids) {
        brandApi.updateShowStatus(showStatus,ids);
    }

    //更新品牌
    public void updateById(Integer id, Brand brand) {
        brand.setId(Convert.toLong(id));
        brandApi.updateById(brand);
    }

    //根据编号查询品牌信息
    public BrandVo getById(Integer id) {
        Brand brand = brandApi.getById(id);
        BrandVo brandVo = new BrandVo();
        BeanUtil.copyProperties(brand,brandVo);
        brandVo.setId(Convert.toInt(id));
        return brandVo;
    }



}
